package cn.edu.zxj.springblog.controller;

import cn.edu.zxj.springblog.model.BlogInfo;
import cn.edu.zxj.springblog.model.Result;
import cn.edu.zxj.springblog.service.BlogInfoService;
import cn.edu.zxj.springblog.utils.JWTUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description：Blog 控制层
 *
 * @author: zxj
 * @date: 2024-02-04
 * @time: 20:06:51
 */
@RestController
@RequestMapping("/blog")
@Slf4j
public class BlogInfoController {
    @Autowired
    private BlogInfoService blogInfoService;

    @RequestMapping("/getList")
    public List<BlogInfo> getList() {
        log.info("接收到获取所有博客信息请求");
        return blogInfoService.getList();
    }

    @RequestMapping("/getBlogDetail")
    public BlogInfo getBlogDetail(Integer blogId, HttpServletRequest request) {
        log.info("接收到获取博客详细信息请求, blogId: {}",blogId);
        // 1. 参数校验
        if (blogId == null ||  blogId < 1) {
            return null;
        }

        // 2. 获取当前登录的Id
        String token = request.getHeader("user_token");
        if (token == null) {
            return null;
        }
        Integer loginId = JWTUtils.getUserIdFromToken(token);

        // 3. 进行服务
        BlogInfo blogInfo = blogInfoService.getBlogDetail(blogId);
        if (blogInfo == null) {
            return null;
        }

        // 4. 设置 LoginUser 字段
        if (loginId != null && loginId.equals(blogInfo.getUserId())) {
            blogInfo.setLoginUser(1);
        }

        return blogInfo;
    }


    @RequestMapping("/add")
    public Boolean add(BlogInfo blogInfo, HttpServletRequest request) {
        log.info("接收到添加博客信息请求, blogInfo: {}",blogInfo);
        // 参数校验
        if (blogInfo == null) {
            return false;
        }
        // 1. 获取 token 中 UserId
        String token = request.getHeader("user_token");
        Claims claims = JWTUtils.parseJWT(token);
        if (claims == null) {
            return false;
        }
        Map<String,Object> map = new HashMap<>(claims);
        Integer userId = (Integer) map.get("id");
        // 2. 完善 blogInfo 中的信息
        blogInfo.setUserId(userId);
        // 3. 处理服务
        Integer ret = blogInfoService.add(blogInfo);
        if (ret == null || ret < 0) {
            return false;
        }

        return true;
    }

    @RequestMapping("/update")
    public Result update(@RequestBody BlogInfo blogInfo) {
        log.info("接收到更新博客信息请求, blogInfo: {}",blogInfo);

        // 参数校验
        if (blogInfo == null) {
            return Result.fail("参数存在问题",false);
        }

        // 业务处理
        Integer ret = blogInfoService.update(blogInfo);
        if (ret == null || ret < 0) {
            return Result.fail("更新博客出现问题",false);
        }
        return Result.success(true);
    }

    @RequestMapping("/delete")
    public Result delete(Integer blogId) {
        log.info("接收到删除博客请求, blogId: {}",blogId);

        // 参数校验
        if (blogId == null) {
            return Result.fail("参数存在问题",false);
        }

        // 业务处理
        Integer ret = blogInfoService.delete(blogId);
        if (ret == null || ret < 0) {
            return Result.fail("删除博客出现问题",false);
        }
        return Result.success(true);
    }

}
